//index.js
//获取应用实例
Page({
  data: {
    week:'3',
    colorArrays: [ "#85B8CF", "#90C652", "#D8AA5A", "#FC9F9D", "#0A9A84", "#61BC69", "#12AEF3", "#E29AAD"],
    wlist: [],
    data:''
  },
  onLoad: function () {
    wx.hideHomeButton({
      success: (res) => {},
    })
    if(wx.getStorageSync('data')==''){
        wx.redirectTo({
          url: '/pages/login/index',
        })
    }else{
    this.data=wx.getStorageSync('data')
    this.setWlist()
    //this.setWeek()//减少腾讯云函数调用次数
    this.setLoaclWeek()
    }
  },
  onPullDownRefresh:function() {
    wx.removeStorageSync('data')
    wx.redirectTo({
      url: '/pages/login/index',
    })
  },
  setLoaclWeek:function(){
      let date=new Date()
      let day=(parseInt((date.getTime()-new Date(this.data.time).getTime())/(1000*60*60*24*7)))//差时，单位：周
      let data=this.data
      
      data.week=data.week+day

      this.setData({data:data})
      this.setWlist()
  },
  setWeek:function(){
    let that=this
    wx.request({
        url: 'https://service-czcepxfq-1300390346.gz.apigw.tencentcs.com/release/table',
        method:'GET',
        success:function(res) {
            let data=that.data
            data.week=res.data

            let date=new Date()
            let day=date.getDay()
            if(day==0){day=7}
            data.time=date.getTime()-(day-1)*(1000*60*60*24)

            wx.setStorageSync('data', data)
            that.setWlist()
        }
      })
  },
  setWlist:function() {
    let list=[]
    let data=this.data
    for (const key in data.data) {
      for (const i in data.data[key]['arrtime']) {
        if(data.data[key]['arrtime'][i]==data.week){
          console.log(data.week)
          list.push({'xqj':(data.data[key]['number']+1)%7,'skjc':(Math.ceil((data.data[key]['number']+1)/7)*2)-1, 'skcd': 2,'kcmc':data.data[key]['course'].slice(0,-4)+' '+data.data[key]['teacher']+' '+data.data[key]['room']})
        }
      }
    }
    this.setData({
      wlist:list,
      week:data.week
    })
  },
  logout:function() {
    wx.removeStorageSync('un')
    wx.removeStorageSync('pwd')
    wx.redirectTo({
      url: '/pages/login/index',
    })
  },
  back:function() {
    let data=this.data
    let week=data.week-1
    if(week!=0){
      let date=new Date()
      let day=date.getDay()
      if(day==0){day=7}
      data.time=date.getTime()-(day-1)*(1000*60*60*24)
      data.week=week
      this.setData({data:data})
      this.setWlist()
      wx.setStorageSync('data', data)
    }
  },
  next:function(params) {
    let data=this.data
    let week=data.week+1
    if(week!=31){
      let date=new Date()
      let day=date.getDay()
      if(day==0){day=7}
      data.time=date.getTime()-(day-1)*(1000*60*60*24)
      data.week=week
      this.setData({data:data})
      this.setWlist()
      wx.setStorageSync('data', data)
    }
  }
})
